﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using BISPOS.Repository;
namespace BISPOS
{
    public partial class SellingReportsUserControl : DevExpress.XtraEditors.XtraUserControl
    {
        InvoiceRepository invoiceRepository = new InvoiceRepository();
        ProductRepository productRepository = new ProductRepository();
        PaymentRepository paymentRepository = new PaymentRepository();
        public SellingReportsUserControl()
        {
            InitializeComponent();
            toDateEdit.DateTime = DateTime.Now.Date;
            fromDateEdit.DateTime = DateTime.Now.Date;
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            searchButton.Text = "ค้นหา..";
            searchButton.Enabled = false;
           try
            {
                creditLinqServerModeSource.QueryableSource = invoiceRepository.GetInvoices(fromDateEdit.DateTime, toDateEdit.DateTime).Where(x=>x.PaymentType.IsCredit).AsQueryable();
                creditSummaryTextEdit.Text = invoiceRepository.GetInvoices(fromDateEdit.DateTime, toDateEdit.DateTime).Where(x => x.PaymentType.IsCredit).Sum(x => x.TotalPrice).ToString("n2");
                
                cashLinqServerModeSource.QueryableSource = invoiceRepository.GetInvoices(fromDateEdit.DateTime, toDateEdit.DateTime).Where(x => !x.PaymentType.IsCredit).AsQueryable();
                cashSummaryTextEdit.Text = invoiceRepository.GetInvoices(fromDateEdit.DateTime, toDateEdit.DateTime).Where(x => !x.PaymentType.IsCredit).Sum(x => x.TotalPrice).ToString("n2");
                cashCancelInvoiceTextEdit.Text = invoiceRepository.GetInActiveInvoices(fromDateEdit.DateTime, toDateEdit.DateTime).Where(x => !x.PaymentType.IsCredit).Sum(x => x.TotalPrice).ToString("n2");

            }
            catch
            {
            
            }
           searchButton.Text = "ตกลง";
           searchButton.Enabled = true;
        }

        private void sellingReportButton_Click(object sender, EventArgs e)
        {
                BISPOS.Report.DailyReport report = new Report.DailyReport();
                Repository.ProductRepository productRepository = new ProductRepository();
                List<Report.SoldProductGroup> soldGroup = new List<Report.SoldProductGroup>();
                foreach(var group in productRepository.AllProductGroups())
                {
                    BISPOS.Report.SoldProductGroup data = new Report.SoldProductGroup(fromDateEdit.DateTime,toDateEdit.DateTime,group.Id);
                    if (data.IsSold)
                        soldGroup.Add(data);
                }
                report.DataSource = soldGroup;
                report.fromDate.Value = fromDateEdit.DateTime.ToShortDateString();
                report.toDate.Value = toDateEdit.DateTime.ToShortDateString();
                report.RequestParameters = false;
                report.ShowPreview();
            
           // BISPOS.Report.SellingReport report = new Report.SellingReport();
           // report.DataSource = productRepository.SoldProductGroup(
           //     DateTime.Parse("1/1/2550"), DateTime.Parse("1/12/2553"));
           // report.ShowPreview(); 
        }

        private void cashReportButton_Click(object sender, EventArgs e)
        {
            IQueryable<Invoice> invoices = (IQueryable<Invoice>)cashLinqServerModeSource.QueryableSource.AsQueryable();
            List<BISPOS.Report.DailyReportProduct> products = new List<Report.DailyReportProduct>();
            foreach(var invoice in invoices.OrderBy(x=>x.IdText))
            {
                foreach(var product in invoice.InvoiceItems.OrderBy(x=>x.Stock.Product.Barcode))
                {
                    string taxed = "";
                    if (!product.Stock.Product.IsTaxed)
                        taxed = "*";

                    string cancel = "";
                    if (!product.Invoice.IsActive)
                        cancel = "#";
                        products.Add(new Report.DailyReportProduct
                        {
                            Date = invoice.Date,
                            Barcode = product.Stock.Product.Barcode,
                            InvoiceId = invoice.IdText,
                            Name = product.Stock.Product.Name,
                            Quantity = product.Quantity,
                            UnitPrice = product.Price.Amount,
                            IsTax = taxed,
                            IsActiveInvoice = cancel
                        });
                    
                }
            }
            BISPOS.Report.DailyClassedReport report = new Report.DailyClassedReport();
            report.DataSource = products;
            report.RequestParameters = false;

            decimal totalIncVatActive = products.Where(x=>x.IsTax == "").Where(x => x.IsActiveInvoice != "#").Sum(x => x.TotalPrice);
            decimal totalExtVatActive = products.Where(x => x.IsTax == "*").Where(x => x.IsActiveInvoice != "#").Sum(x => x.TotalPrice);

            report.sumExtVat.Value = products.Where(x=>x.IsTax == "*").Where(x => x.IsActiveInvoice != "#").Sum(x => x.TotalPrice).ToString("N2");
            report.vat.Value = (totalIncVatActive * (decimal)(7.0000 / 107.0000)).ToString("N2");
            report.sumIncVat.Value = (totalIncVatActive - (totalIncVatActive * (decimal)(7.0000 / 107.0000))).ToString("N2");
            report.totalCancel.Value = products.Where(x => x.IsActiveInvoice == "#").Sum(x => x.TotalPrice).ToString("N2");
            report.type.Value = "เงินสด";
            report.net.Value = (products.Sum(x => x.TotalPrice) - products.Where(x => x.IsActiveInvoice == "#").Sum(x => x.TotalPrice)).ToString("N2");


            report.fromDate.Value = fromDateEdit.DateTime.ToShortDateString();
            report.toDate.Value = toDateEdit.DateTime.ToShortDateString();
            report.ShowPreview();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {

            IQueryable<Invoice> invoices = (IQueryable<Invoice>)creditLinqServerModeSource.QueryableSource.AsQueryable(); 
            List<BISPOS.Report.DailyReportProduct> products = new List<Report.DailyReportProduct>();
            foreach (var invoice in invoices.OrderBy(x => x.IdText))
            {
                foreach (var product in invoice.InvoiceItems.OrderBy(x => x.Stock.Product.Barcode))
                {
                    Report.DailyReportProduct tempP = new Report.DailyReportProduct();

                    tempP = new Report.DailyReportProduct
                       {
                           Date = invoice.Date,
                           Barcode = product.Stock.Product.Barcode,
                           InvoiceId = invoice.IdText,
                           Name = product.Stock.Product.Name,
                           Quantity = product.Quantity,
                           UnitPrice = product.Price.Amount
                       };
                    products.Add(tempP);
                }
            }
            
            BISPOS.Report.DailyClassedReport report = new Report.DailyClassedReport();
          
            report.DataSource = products.Distinct().ToList();
            report.RequestParameters = false;
            report.type.Value = "เงินเชื่อ";



            report.fromDate.Value = fromDateEdit.DateTime.ToShortDateString();
            report.toDate.Value = toDateEdit.DateTime.ToShortDateString();
            report.ShowPreview();
        }

    }
}
